package com.cy.milktea.service.impl;

import com.cy.milktea.common.annotation.SetUserAndTime;
import com.cy.milktea.mapper.DrinkEntremetsMapper;
import com.cy.milktea.mapper.EntremetsMapper;
import com.cy.milktea.pojo.Entremets;
import com.cy.milktea.service.EntremetsService;
import com.cy.milktea.utils.ImageUtils;
import com.cy.milktea.utils.PageHelperUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service
public class EntremetsServiceImpl implements EntremetsService {

    @Autowired
    private EntremetsMapper entremetsMapper;
    @Autowired
    private DrinkEntremetsMapper drinkEntremetsMapper;

    @SetUserAndTime("all")
    @Override
    public void add(Entremets entremets) {
        if (entremets.getFile() == null) throw new RuntimeException("图片为空!");
        String imageUrl = ImageUtils.saveImage(entremets.getFile());
        entremets.setImage(imageUrl);
        try {
            entremetsMapper.insert(entremets);
        } catch (Exception e) {
            //插入异常时回滚图片
            ImageUtils.deleteImage(imageUrl);
            throw e;
        }
    }

    @Transactional
    @Override
    public void delete(Integer id, Integer force) {
        //判断是否为强制删除操作
        if (force == 1) {
            //删除中间表的关系
            drinkEntremetsMapper.deleteByEntremetsId(id);
        } else {
            //删除前先查询是否有饮品含有该配料
            if (drinkEntremetsMapper.selectByEntremetsId(id) > 0)
                throw new RuntimeException("有饮品含有该配料,不可删除!");
        }
        //查询出图片地址
        String imageUrl = entremetsMapper.selectIamgeById(id);
        //删除配料
        if (entremetsMapper.delete(id) == 1)
            ImageUtils.deleteImage(imageUrl);
    }

    @SetUserAndTime("modify")
    @Override
    public void update(Entremets entremets) {
        //判断是否更新图片
        if (entremets.getFile() != null
                && !entremets.getFile().isEmpty()
                && StringUtils.hasLength(entremets.getImage())) {
            //旧图片url
            String oldImageUrl = entremets.getImage();
            //添加新图片
            String newImageUrl = ImageUtils.saveImage(entremets.getFile());
            entremets.setImage(newImageUrl);
            try {
                //更新
                if (entremetsMapper.update(entremets) != 1) throw new RuntimeException("更新失败!");
                //更新成功删除旧图片
                ImageUtils.deleteImage(oldImageUrl);
            } catch (Exception e) {
                //更新失败回滚新图片
                ImageUtils.deleteImage(newImageUrl);
            }
        } else {
            //防止脏图片路径
            entremets.setImage(null);
            entremetsMapper.update(entremets);
        }
    }

    @Override
    public PageInfo<Entremets> query(String name, Integer status) {
        return PageHelperUtils.startPage().doSelectPageInfo(()->entremetsMapper.select(name,status));
    }
}
